package pipe

import (
	"gitee.com/injoyai/goutil/g"
	"gitee.com/injoyai/goutil/net/conn/v2"
	"gitee.com/injoyai/goutil/oss/io"
	"log"
)

// WriteWithBase64 数据通讯加密,通道客户端服务端的发送数据函数
func WriteWithBase64(req []byte) []byte {
	req = []byte(g.EncodeBase64(req))
	req = io.WriteWithDefault(req)
	return req
}

// ReadWithBase64 数据通讯解密,通道客户端服务端的读取数据函数
func ReadWithBase64(c *io.Client) (bytes []byte, err error) {
	bytes, err = io.ReadWithDefault(c)
	if err != nil {
		return nil, err
	}
	if len(bytes) > 2+2 {
		bytes = bytes[2 : len(bytes)-2]
	}
	return g.DecodeBase64(string(bytes))
}

// PrintWithPipe 通道服务端打印函数
func PrintWithPipe(tag string, msg *conn.Message) {
	printWithPipe(tag, msg.Key(), msg.String())
}

func printWithPipe(tag, addr, msg string) {
	log.Printf("[pipe][%s][%s] %s\n", tag, addr, msg)
}

//func NewWriteAESCBC(key []byte, iv ...[]byte) func([]byte) []byte {
//	return func(req []byte) []byte {
//		req, _ = aes.EncryptCBC(req, key, iv...)
//		return io.WriteWithDefault(req)
//	}
//}
//
//func NewReadAESCBC(key []byte, iv ...[]byte) func(c *io.Client) (bytes []byte, err error) {
//	return func(c *io.Client) (bytes []byte, err error) {
//		bytes, err = io.ReadWithDefault(c)
//		if err != nil {
//			return nil, err
//		}
//		return aes.DecryptCBC(bytes, key, iv...)
//	}
//}

//type P struct {
//	sn   string     //
//	conn *maps.Safe //代理的客户端连接
//
//}
//
//func Do(c *tcp.Client, bytes []byte) error {
//	m, err := decodeMsg(bytes)
//	if err != nil {
//		return err
//	}
//	switch m.Type {
//	case Connect:
//		_, err = this.connect(m.Key, m.Addr)
//	case Write:
//		err = this.write(m.Key, m.Addr, m.Data)
//	case Close:
//		this.close(m.Key)
//	}
//	return nil
//}
//
//type C struct {
//	Debug     bool
//	Timeout   time.Duration
//	CloseFunc func(msg *conn.Message) //关闭事件函数
//	DealFunc  func(msg *conn.Message) //处理数据函数
//	PrintFunc func(tag string, msg *io.Message)
//	WriteFunc func([]byte) []byte
//	ReadFunc  func(*io.Client) ([]byte, error)
//}
//
//func (this *C) Connect(key, addr string) (io.ReadWriteCloser, error) {
//	c, err := tcp.Dial(addr)
//	if err == nil {
//		c.SetTimeout(this.Timeout)
//		c.SetKey(key)
//		c.SetPrintFunc(this.PrintFunc)
//		c.Debug(this.Debug)
//		c.SetWriteFunc(this.WriteFunc)
//		c.SetReadFunc(this.ReadFunc)
//		c.SetCloseFunc(this.CloseFunc)
//		c.SetDealFunc(this.DealFunc)
//	}
//	return c, err
//}
